﻿using Aspose.Cells;
using CheccCoverplateCulvertAssemble.Common;
using CheccCoverplateCulvertAssemble.Common.Http;
using CheccCoverplateCulvertAssemble.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CheccCoverplateCulvertAssemble.FormColl
{
    public partial class UnitaryRebarForm : Form
    {
        private string _generalDiagram;
        private string _clearSpanAndHeight;
        private double _depthOfFill;
        private string _netProtectiveLayer;
        public UnitaryRebarForm(string generalDiagram, string clearSpanAndHeight, double depthOfFill, string netProtectiveLayer)
        {
            #region 界面设置
            //使最大化窗口失效
            this.MaximizeBox = false;
            //禁止对窗口大小进行拖拽
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            //窗口位置
            this.StartPosition = FormStartPosition.CenterScreen;
            //窗口置顶
            this.TopMost = true;
            #endregion
            InitializeComponent();
            //窗体控件适应屏幕分辨率
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this._generalDiagram = generalDiagram;
            this._clearSpanAndHeight = clearSpanAndHeight;
            this._depthOfFill = depthOfFill;
            this._netProtectiveLayer = netProtectiveLayer;
            this.Icon = Properties.Resources.Culvert;
        }

        private void UnitaryRebarForm_Load(object sender, EventArgs e)
        {
            #region 洞身基础钢筋参数赋值
            SetDataGridViewData(_generalDiagram, _clearSpanAndHeight, _depthOfFill, _netProtectiveLayer);
            #endregion
            dgv_Rebar1.ClearSelection();
            dgv_Rebar2.ClearSelection();
            dgv_Rebar2_1.ClearSelection();
            dgv_Rebar3.ClearSelection();    
            dgv_Rebar4.ClearSelection();
            dgv_Rebar5.ClearSelection();
            dgv_Rebar6.ClearSelection();    
            dgv_Rebar1.Enabled = false;
            dgv_Rebar2.Enabled = false;
            dgv_Rebar2_1.Enabled  = false;
            dgv_Rebar3.Enabled = false;
            dgv_Rebar4.Enabled = false;
            dgv_Rebar5.Enabled = false;
            dgv_Rebar6.Enabled = false;
        }
        private void SpiltTh(string depthOfFillTh, out double start, out double end)
        {
            //拆解填土高度
            string newDepthOfFillTh = "";
            for (int i = 1; i < depthOfFillTh.Length - 1; i++)
            {
                newDepthOfFillTh += depthOfFillTh[i];
            }
            string[] ttgd = newDepthOfFillTh.Split(',');
            if (ttgd.Length < 2)
                ttgd = newDepthOfFillTh.Split('，');
            if (depthOfFillTh[0].ToString() == "(")
                start = Convert.ToDouble(ttgd[0]) + 0.0001;
            else
                start = Convert.ToDouble(ttgd[0]);
            if (depthOfFillTh[depthOfFillTh.Length - 1].ToString() == ")")
                end = Convert.ToDouble(ttgd[1]) - 0.0001;
            else
                end = Convert.ToDouble(ttgd[1]);
        }
        private void SetDataGridViewData(string generalDiagram, string clearSpanAndHeight, double depthOfFill, string netProtectiveLayer)
        {
            //1号钢筋
            HttpRequestData httpRequestData = HttpRequestData.CreateHttpRequestData();
            List<MonolithicNo1Rebar> monolithicNo1Rebars = httpRequestData.SelectAssembleMonolithicNo1s(CommonData.Instance.CommomTotal.GeneralDiagram);
            foreach (var item in monolithicNo1Rebars)
            {
                SpiltTh(item.fillHeight, out double min, out double max);
                if (depthOfFill >= min && depthOfFill <= max)
                {
                    if (item.standardDrawing == generalDiagram && item.clearSpan == clearSpanAndHeight)
                    {
                        dgv_Rebar1.Rows.Add(item.barMargin, item.rootNumber, item.reinforcementType, item.diameterReinforcement);
                    }
                }
            }

            //2号钢筋
            List<MonolithicNo2Rebar> monolithicNo2Rebars = httpRequestData.SelectAssembleMonolithicNo2s(CommonData.Instance.CommomTotal.GeneralDiagram);
            foreach (var item in monolithicNo2Rebars)
            {
                SpiltTh(item.fillHeight, out double min, out double max);
                if (depthOfFill >= min && depthOfFill <= max)
                {
                    if (item.standardDrawing == generalDiagram && item.clearSpan == clearSpanAndHeight)
                    {
                        dgv_Rebar2.Rows.Add(item.marginNo2, item.rootsPerMeterTwo, item.diameterReinforcement, item.reinforcementType, item.lengthOfNo2);
                    }
                }
            }
            //2'号钢筋
            List<MonolithicNo2pRebar> monolithicNo2pRebars = httpRequestData.SelectAssembleMonolithicNo2p(CommonData.Instance.CommomTotal.GeneralDiagram);
            double Angle = 45;

            foreach (var item in monolithicNo2pRebars)
            {
                SpiltTh(item.fillHeight, out double min, out double max);
                if (depthOfFill >= min && depthOfFill <= max)
                {
                    if (item.clearSpan == clearSpanAndHeight)
                    {
                        if (Angle == 5)
                        {
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree5, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                        }
                        else if (Angle == 10)
                        {
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree10, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                        }
                        else if (Angle == 15)
                        {
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree15, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                        }
                        else if (Angle == 20)
                        {
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree20, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                        }
                        else if (Angle == 25)
                        {
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree25, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                        }
                        else if (Angle == 30)
                        {
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree30, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                        }
                        else if (Angle == 35)
                        {
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree35, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                        }
                        else if (Angle == 40)
                        {
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree40, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                        }
                        else
                            dgv_Rebar2_1.Rows.Add(item.marginNo2p, item.degree45, item.diameterReinforcement, item.reinforcementType, dgv_Rebar2.Rows[0].Cells[4].Value);
                    }
                }
            }
            //3号钢筋
            List<MonolithicNo3Rebar> monolithicNo3Rebars = httpRequestData.SelectAssembleMonolithicNo3s(CommonData.Instance.CommomTotal.GeneralDiagram);
            foreach (var item in monolithicNo3Rebars)
            {
                SpiltTh(item.fillHeight, out double min, out double max);
                if (depthOfFill >= min && depthOfFill <= max)
                {
                    if (item.standardDrawing == generalDiagram && item.clearSpan == clearSpanAndHeight)
                    {
                        dgv_Rebar3.Rows.Add(item.insertionBaseDepth, item.rootsPerMeterThree, item.reinforcementType, item.diameterReinforcement, item.barLength);
                    }
                }
            }
            //4号钢筋
            List<MonolithicNo4Rebar> monolithicNo4Rebars = httpRequestData.SelectAssembleMonolithicNo4s(CommonData.Instance.CommomTotal.GeneralDiagram);
            foreach (var item in monolithicNo4Rebars)
            {
                SpiltTh(item.fillHeight, out double min, out double max);
                if (depthOfFill >= min && depthOfFill <= max)
                {
                    if (item.standardDrawing == generalDiagram && item.clearSpan == clearSpanAndHeight)
                    {
                        dgv_Rebar4.Rows.Add(item.reinforcementSpacing, item.reinforcementType, item.diameterReinforcement, item.barLength);
                    }
                }
            }
            //5号钢筋
            List<MonolithicNo5Rebar> monolithicNo5Rebars = httpRequestData.SelectAssembleMonolithicNo5s(CommonData.Instance.CommomTotal.GeneralDiagram);
            foreach (var item in monolithicNo5Rebars)
            {
                SpiltTh(item.fillHeight, out double min, out double max);
                if (depthOfFill >= min && depthOfFill <= max)
                {
                    if (item.standardDrawing == generalDiagram && item.clearSpan == clearSpanAndHeight)
                    {
                        dgv_Rebar5.Rows.Add(item.ipsilateralSpacing, item.reinforcementType, item.diameterReinforcement, item.numberBars);
                    }
                }
            }
            //6号钢筋
            List<MonolithicNo6Rebar> monolithicNo6Rebars = httpRequestData.SelectAssembleMonolithicNo6s(CommonData.Instance.CommomTotal.GeneralDiagram);
            foreach (var item in monolithicNo6Rebars)
            {
                SpiltTh(item.fillHeight, out double min, out double max);
                if (depthOfFill >= min && depthOfFill <= max)
                {
                    if (item.standardDrawing == generalDiagram && item.clearSpan == clearSpanAndHeight)
                    {
                        dgv_Rebar6.Rows.Add(netProtectiveLayer, item.reinforcementType, item.numberRootsPerMeter, item.diameterReinforcement);
                    }
                }
            }
        }

        private void UnitaryRebarForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //RebarEntity rebarData = RebarEntity.RebarEntityObj();
            //rebarData.dgv_rebar1 = convertDgv(this.dgv_Rebar1);
            //rebarData.dgv_Rebar2 = convertDgv(this.dgv_Rebar2);
            //rebarData.dgv_Rebar2_1 = convertDgv(this.dgv_Rebar2_1);
            //rebarData.dgv_Rebar3 = convertDgv(this.dgv_Rebar3);
            //rebarData.dgv_Rebar4 = convertDgv(this.dgv_Rebar4);
            //rebarData.dgv_Rebar5 = convertDgv(this.dgv_Rebar5);
            //rebarData.dgv_Rebar6 = convertDgv(this.dgv_Rebar6);
        }

        private DataGridView convertDgv(DataGridView dgv)
        {
            DataGridView dataGridView = new DataGridView();
            dataGridView.RowHeadersVisible = false;
            dataGridView.AllowUserToAddRows = false;
            foreach (DataGridViewColumn c in dgv.Columns)
            {
                dataGridView.Columns.Add(c.Clone() as DataGridViewColumn);
            }
            foreach (DataGridViewRow r in dgv.Rows)
            {
                int index = dataGridView.Rows.Add(r.Clone() as DataGridViewRow);
                foreach (DataGridViewCell o in r.Cells)
                {
                    dataGridView.Rows[index].Cells[o.ColumnIndex].Value = o.Value;
                }
            }
            return dataGridView;
        }
    }
}
